/**
 * Created by Administrator on 2017/2/22.
 */

import {allUrl} from '../scripts/url'
import {Toast,checkLogin,getFormatDateByLong} from '../scripts/tip'


export const  initProductDetails =(productId) => {

    $.getJSON(''+allUrl()+'mall/queryProductDetail?productId='+productId+'', {
        sinceId:0,
        pageSize: 10
    }, (data) => {
        checkLogin(data, false, '');

        var product=data.body.product;
        var slide=product.productSlidelUrlList;
        var productDetailUrlList=product.productDetailUrlList;

        getDetails(product);

        productSlide(slide);

        productinfor(productDetailUrlList);
    })
};


//TODO slide
export const  productSlide =(slide) => {

    var str='';
    for(var i=0;i<slide.length;i++){
        str+='<li class="swiper-slide">' +
            '<a href="javascript:;">' +
            '<img  src="'+slide[i]+'"/> ' +
            '</a>' +
            '</li>'
    }
    $('.BannerWrap  .Banner').html(str);

    var mySwiper = new Swiper('.swiper-container',{
        pagination: '.pagination',
        autoplay: 5000,
        loop:true,
        grabCursor: true
    });

}


//TODO product
function getDetails(product){

    var postageFlag=product.postageFlag;
    var postage='包邮';
    var post=product.productPrice-product.productRealPrice;
    var productId=product.productId;
    var productType=product.productType;

    if(postageFlag===0)  postage='￥'+post+'';

    $('.TextDetails .property h1').html(product.productName);
    $('.TextDetails .property .price .productPrice').html('￥'+product.productPrice+'');
    $('.TextDetails .property .price .price_p').html('￥'+product.category[0].detailList[0].voucherLimit+'');
    $('.TextDetails .freightSale .postage').html('运费：'+postage+'');
    $('.TextDetails .freightSale .soldCount').html('销量：'+product.soldCount+'笔');


    if(productType===1){
        $('.product-dialog-title h1 a:first-child').html('选择套餐类型');
        $('.product-models h2').html('套餐分类');
        $('.product-size h2').html('发团时间段');
    }

    var $dLength=product.category[0].detailList.length;

    if($dLength>1){
        if(productType===1){
            $('.select_product').html('选择套餐')
        }else {
            $('.select_product').html('选择类型')
        }
    }else {

        $('body .select_product').html('已选择' +
            '"<span class="s-size" productDetailId="'+product.category[0].detailList[0].productDetailId+'" >'+product.category[0].colorOrType+'</span>" ' +
            '"<span class="s-model"">'+product.category[0].detailList[0].size+'</span>" ' +
            '"<span class="s-number">'+1+'</span>"个')
    }


    //TODO select
    $('.select_product').click(function(){
        var openDailog= layer.open({
            type: 1,
            anim: 'down',
            //shadeClose: false,
            content: $('.product-dialog').html(),
            style: 'position:fixed; bottom:3.3rem; z-index:999999;width:100%;left:0;border-top-left-radius:6px;border-top-right-radius:6px;border-bottom-left-radius:0;border-bottom-right-radius:0;background:#fff',
            success:function(){

                var v=$('input[type=tel]');

                function isOne(){
                    if(v.val()==='1'){
                        $('.minus').addClass('noClick')
                    }else {
                        $('.minus').removeClass('noClick')
                    }
                }
                isOne();

                $('.plus').click(function(){

                    if(productType===1){

                        Toast('数量只能为1',2000,'');
                    }else {
                        if($('.vision  li.sel').length>0){

                            v.val(parseInt(v.val())+1);

                            isOne();

                            $('.s-number').html( $('.btn-input input').val());

                        }else {
                            //Toast('完善商品信息',2000,'');
                        }
                    }
                });

                $('.minus').click(function(){

                    if($('.vision   li.sel').length>0){

                        v.val(parseInt(v.val())-1);
                        if(parseInt(v.val())<1){
                            v.val(1);
                        }
                        isOne();

                        $('.s-number').html( $('.btn-input input').val());
                    }
                });

                var category=product.category;

                productPop(product);
            }
        })
    });

    //TODO buy

    $('.buyBtn ').click(function(){

         var $vlength= $('.vision li.sel').length;

          var ProductDetailId,count;

           if($('.s-size').length>0){

              ProductDetailId=$('.s-size').attr('productDetailId');

               count=parseInt($('.s-number').html());

               location.href='#/mall/presetOrderDetail?productId='+productId+'&productDetailId='+ProductDetailId+'&count='+count+''
           }
           else{
            if($('.layui-m-layer ').length>0){

                if($vlength<=0){

                    //Toast('请完善商品信息',2000,'');

                }else {
                   ProductDetailId=$('.s-size').attr('productDetailId');

                    count=parseInt($('.s-number').html());

                    location.href='#/mall/presetOrderDetail?productId='+productId+'&productDetailId='+ProductDetailId+'&count='+count+''
                }
            }else{
                var openDailog1= layer.open({
                    type: 1,
                    anim: 'down',
                    //shadeClose: false,
                    content: $('.product-dialog').html(),
                    style: 'position:fixed; bottom:3.3rem; z-index:999999;width:100%;left:0;border-top-left-radius:6px;border-top-right-radius:6px;border-bottom-left-radius:0;border-bottom-right-radius:0;background:#fff',
                    success:function(){

                        var v=$('input[type=tel]');

                        function isOne(){
                            if(v.val()==='1'){
                                $('.minus').addClass('noClick')
                            }else {
                                $('.minus').removeClass('noClick')
                            }
                        }
                        isOne();

                        $('.plus').click(function(){

                          if(productType===1){
                              Toast('数量只能为1',2000,'');
                          }else {
                              if($('.vision   li.sel').length>0){

                                  v.val(parseInt(v.val())+1);

                                  isOne();

                                  $('.s-number').html( $('.btn-input input').val());

                              }else {
                                  //Toast('完善商品信息',2000,'');
                              }
                          }
                        });

                        $('.minus').click(function(){

                            if($('.vision   li.sel').length>0){
                                v.val(parseInt(v.val())-1);
                                if(parseInt(v.val())<1){
                                    v.val(1);
                                }
                                isOne();

                                $('.s-number').html( $('.btn-input input').val());
                            }
                        });

                        productPop(product);
                    }
                })
            }
           }
        })
   }


 //TODO product Pop
 function productPop(product){
    var category=product.category;
    var categoryStr='';
   var productType=product.productType;

    for(var i=0;i<category.length;i++){
        var className='';
        if($('.select_product   .s-size').attr('$indexId')){

            var indexId=parseInt($('.select_product .s-size').attr('$indexId'));

            if(i===indexId)  className='sel';

        }else {
           if(i===0)  className='sel'
        }
        categoryStr+='<li class="'+className+'">'+category[i].colorOrType+'</li>'
    }

        $('.product-models .models').html(categoryStr);


        if($('.select_product  .s-size').attr('$indexId')){

            var $indexId=parseInt($('.select_product   .s-size').attr('$indexId'));

            ProductDetailList($indexId,category)

        }else {
            ProductDetailList(0,category)
        }

            if($('.s-number').length>0){

                $('input[type=tel]').val($('.s-number').html())
            }else {
                $('input[type=tel]').val(1)
            }


        $('.models li').click(function(){

            $('.product-size .vision').empty();

            $('.btn-input input').val(1);

            var html=$(this).html();

            var $index=$(this).index();

            $(this).finish().addClass('sel').siblings().removeClass('sel');

            var $dLength=product.category[0].detailList.length;

            if($dLength>1){

                if(productType===1){
                    $('.select_product').html('选择套餐')
                }else {
                    $('.select_product').html('选择类型')
                }
                $('.TextDetails .property .price .productPrice').html('￥'+product.productPrice+'');

                ProductDetailList($index,category);

            }else {
                $('body .select_product').html('已选择' +
                    '"<span class="s-size" $indexId="'+$index+'" $pIndexId="'+0+'" productDetailId="'+product.category[$index].detailList[0].productDetailId+'" >'+product.category[$index].colorOrType+'</span>" ' +
                    '"<span class="s-model"">'+product.category[$index].detailList[0].size+'</span>" ' +
                    '"<span class="s-number">'+1+'</span>"个');

                ProductDetailList($index,category);
            }
        });
    }


 //TODO product Pop details
 function ProductDetailList($index,category){

    var detailList=category[$index].detailList;
    var detailListStr='';
    var $length=detailList.length;
     var className='';
     var clickName='canClick';
     var remaindCount;
    if($length===1){
        remaindCount=detailList[0].remaindCount;

        detailListStr+='<li class="sel canClick">'+detailList[0].size+'</li>';

        if(remaindCount<=0)  detailListStr+='<li class="sel forbidClick">'+detailList[0].size+'</li>'


    }else {
           for(var j=0;j<$length;j++){

                remaindCount=detailList[j].remaindCount;

               if(remaindCount<=0) {
                   clickName='forbidClick';
               }else {
                   clickName='canClick';
               }

               if($('.select_product  .s-size').attr('$pIndexId')){

                   var pIndexId=parseInt($('.select_product .s-size').attr('$pIndexId'));

                   if(j===pIndexId){
                       className='sel'
                   }else {
                       className=''
                   }
               }
               detailListStr+='<li class="'+className+' '+clickName+'" productDetailId="'+detailList[j].productDetailId+'" >'+detailList[j].size+'</li>'
           }
    }

    $('.product-size .vision').html(detailListStr);


    $('.vision li.canClick').click(function(){

        $('.btn-input input').val(1);

        var $pIndex=$(this).index();

        var productDetailId=$(this).attr('productDetailId');

        $(this).finish().addClass('sel').siblings().removeClass('sel');

        $('body .select_product').

        html(' 已选择"<span class="s-size" $indexId="'+$index+'" $pIndexId="'+$pIndex+'" productDetailId="'+productDetailId+'" >'+category[$index].colorOrType+'</span>" ' +
            '"<span class="s-model" >'+$(this).html()+'</span>" ' +
            '"<span class="s-number">1</span>个"');

        if($('.s-size').attr('$pIndexId')){

            var index2=$pIndex+1;

            $('.vision li:nth-child('+index2+')').addClass('sel').siblings().removeClass('sel');
        }

        $('.productPrice').html('￥'+detailList[$pIndex].productRealPrice+'');

        $('.p-num').html(detailList[$pIndex].voucherLimit);

    })
}


 //TODO product productDetailUrlList
 function productinfor(productDetailUrlList){

    var str='';

    for(var i=0;i<productDetailUrlList.length;i++){

      str+=' <img  src="'+productDetailUrlList[i]+'"/>'
    }
    $('.ProductImg').html(str);


}
